package cn.yunhe.dao.impl;

import cn.yunhe.dao.IEmpDao;
import cn.yunhe.eneity.Emp;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Component
public class EmpDao implements IEmpDao {
    private DataSource dataSource;
    @Override
    public List<Emp> getEmps() {
        List<Emp> list=new ArrayList<Emp>();
        try {
            Connection conn=dataSource.getConnection();
            String sql="select * from emp";
            PreparedStatement ps=conn.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                Emp e=new Emp();
                e.setEmpno(rs.getInt("empon"));
                e.setEname(rs.getString("ename"));
                e.setJob(rs.getString("job"));
                list.add(e);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public DataSource getDataSource(){
        return dataSource;
    }
    public void setDataSource(DataSource dataSource){

        this.dataSource=dataSource;
    }


}
